import os.path
import secrets

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_login import LoginManager, login_required, current_user, logout_user
from matplotlib.contour import ContourSet

from settings import DevelopmentConfig, ProductionConfig, Config
from extensions import init_plugs
import os

config = {
    'dev': DevelopmentConfig,
    'prod': ProductionConfig,
}

def create_app():
    app = Flask(__name__)

    app.config['SECRET_KEY'] = secrets.token_urlsafe(16)
    Config = config['dev']

    # read the setting files
    app.config.from_object(Config)
    app.template_folder = os.path.abspath('templates')
    app.static_folder = os.path.abspath('static')

    # initialize various plugins
    init_plugs(app)

    from .words import compkey_blue
    from .users import user_blue
    from .views import index_bp
    from .gpt import gpt
    from .comments import comment_blue

    import models
    
    # Register Blueprints

    app.register_blueprint(compkey_blue, url_prefix='/compkey')
    app.register_blueprint(user_blue, url_prefix='/user')
    app.register_blueprint(gpt, url_prefix='/gpt')
    app.register_blueprint(comment_blue, url_prefix='/comment')
    app.register_blueprint(index_bp)

    return app